01 / 07

List all Access Modifiers (public, private, protected, internal/package-private).

Difficulty: 1/10

Access Modifiers

Access modifiers control the visibility and accessibility of classes and their members. In Java, the primary access levels are public, protected, package-private, and private. C# provides public, private, protected, internal, protected internal, and private protected. As a senior engineer, I treat access modifiers as part of API and encapsulation design: the narrowest visibility that satisfies the requirement is generally preferable.

javascript
  1. 1

    public: accessible wherever the language's visibility rules permit.

  2. 2

    private: restricted to the declaring class/type.

  3. 3

    protected: accessible to the declaring type and appropriate derived types.

  4. 4

    Java package-private: accessible within the same package.

  5. 5

    C# internal: accessible within the same assembly.

  6. 6

    C# private protected: accessible to derived types within the same assembly.

  7. 7

    Use the least visibility necessary to preserve encapsulation.

Follow-up Questions

  • What is the difference between protected and package-private?
  • What is internal in C#?
  • Can a private member be accessed by a derived class?
  • What is private protected in C#?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.